package com.imooc.service.impl;

import com.imooc.mapper.StuMapper;
import com.imooc.pojo.Stu;
import com.imooc.service.StuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author lihongyu
 * @CreateTime 2021-08-03 10:10
 * @Description 学生业务逻辑实现类(测试事务)
 */
@Service
public class StuServiceImpl implements StuService {

    @Resource
    private StuMapper stuMapper;


    /**
     * 保存学生
     */
    @Override
    public void saveStu() {
        Stu stu = new Stu();
        stu.setName("parent");
        stu.setAge(100);
        stuMapper.insert(stu);
    }

    /**
     * 修改学生信息
     *
     * @param id 学生编码
     */
    @Override
    public void updateStu(Long id) {

    }

    /**
     * 删除学生信息
     *
     * @param id 学生编码
     */
    @Override
    public void deleteStu(Long id) {

    }

    /**
     * 查询学生信息
     *
     * @param id 学生编码
     * @return 学生信息实体
     */
    @Override
    public Stu getStu(Long id) {
        return null;
    }

    /**
     * 保存主数据
     */
    @Override
    public void saveParentData() {
        Stu stu = new Stu();
        stu.setName("parent");
        stu.setAge(100);
        stuMapper.insert(stu);
    }

    /**
     * 保存子数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.MANDATORY)
    public void saveChildData() {
        Stu stu = new Stu();
        stu.setName("child-1");
        stu.setAge(111);
        stuMapper.insert(stu);
        int i = 1/0;
        Stu stu2 = new Stu();
        stu2.setName("child-2");
        stu2.setAge(222);
        stuMapper.insert(stu2);
    }
}
